//
//  QYLogFileManager.m
//  QYQYAssistant
//
//  Created by hle2 on 2021/12/10.
//

#import "HLELogFileManager.h"
#import <YYCategories/YYCategories.h>

@interface HLELogFileManager ()

@end

@implementation HLELogFileManager

+ (HLELogFileManager *)sharedInstance {
    static dispatch_once_t onceToken;
    static HLELogFileManager *instance = nil;
    dispatch_once(&onceToken, ^{
        instance = [[HLELogFileManager alloc] init];
        
    });
    return instance;
}

+ (void)clean {
    NSError *error;
    NSString *content =[NSString stringWithContentsOfFile:[self sharedInstance].filePath encoding:NSUTF8StringEncoding error:&error];
    if (!error) {
        // NSLog(@"文件读取成功: %zu",content.length);
    }else{
        // NSLog(@"%@",error.localizedDescription);
    }
    NSString *tempStr = [NSString stringWithFormat:@"%@",@""];

    BOOL res = [tempStr writeToFile:[self sharedInstance].filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    if (res) {
        // NSLog(@"INFO写入成功");
    }else {
        // NSLog(@"INFO写入失败");
    }
}

+ (void)cleanData {
    NSError *error;
    NSString *content =[NSString stringWithContentsOfFile:[self sharedInstance].filePathData encoding:NSUTF8StringEncoding error:&error];
    if (!error) {
        // NSLog(@"文件读取成功: %zu",content.length);
    }else{
        // NSLog(@"%@",error.localizedDescription);
    }
    NSString *tempStr = [NSString stringWithFormat:@"%@",@""];

    BOOL res = [tempStr writeToFile:[self sharedInstance].filePathData atomically:YES encoding:NSUTF8StringEncoding error:nil];
    if (res) {
        // NSLog(@"INFO写入成功");
    }else {
        // NSLog(@"INFO写入失败");
    }
}

+ (BOOL)writeString:(NSString *)string {
    NSError *error;
    NSString *content =[NSString stringWithContentsOfFile:[self sharedInstance].filePath encoding:NSUTF8StringEncoding error:&error];
    if (!error) {
        // NSLog(@"文件读取成功: %zu",content.length);
    }else{
        // NSLog(@"%@",error.localizedDescription);
    }
    NSString *tempStr =[NSString stringWithFormat:@"%@\n%@:%@",content,[NSDate date].stringWithISOFormat, string];
    BOOL res = [tempStr writeToFile:[self sharedInstance].filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    if (res) {
        // NSLog(@"INFO写入成功");
    }else {
        // NSLog(@"INFO写入失败");
    }
    return res;
}

+ (BOOL)writeData:(NSData *)data {
    NSData *content = [NSData dataWithContentsOfFile:[self sharedInstance].filePathData];
    NSMutableData *tempData = [NSMutableData  dataWithData:content];
    [tempData appendData:data];
    BOOL res = [tempData writeToFile:[self sharedInstance].filePathData atomically:YES];
    if (res) {
        // NSLog(@"INFO写入成功");
    }else {
        // NSLog(@"INFO写入失败");
    }
    return res;
}

- (NSString *)filePath {
    if (!_filePath) {
        NSString *documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        //log.txt文件
        _filePath = [documentsDir stringByAppendingPathComponent:@"log.txt"];
        
        NSFileManager *fileManager = [NSFileManager defaultManager];
        BOOL isExit = [fileManager fileExistsAtPath:_filePath];
        //文件夹是否存在
        if (!isExit) {
            // // NSLog(@"log文件不存在");
            [fileManager createFileAtPath:documentsDir contents:nil attributes:nil];
        }
    }
    return _filePath;
}

- (NSString *)filePathData {
    if (!_filePathData) {
        NSString *documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
        //log.txt文件
        _filePathData = [documentsDir stringByAppendingPathComponent:@"logData.txt"];
        
        NSFileManager *fileManager = [NSFileManager defaultManager];
        BOOL isExit = [fileManager fileExistsAtPath:_filePathData];
        //文件夹是否存在
        if (!isExit) {
            // NSLog(@"log文件不存在");
            [fileManager createFileAtPath:documentsDir contents:nil attributes:nil];
        }
    }
    return _filePathData;
}

@end
